//
//  TempClipFailListVC.swift
//  ANOASTORE
//
//  Created by hudan on 2025/5/29.
//  Copyright © 2025 zwb. All rights reserved.
//

// 失败切片列表

import Foundation

final class TempClipFailListVC: BaseTableViewController {
    
    private var dataArray = [OffLineModel]()
    
//    private lazy var noDataView: UIView = {
//        let view = UIView().extFrameSize(size: CGSize(width: KS_SCREEN_WIDTH, height: KS_SCREEN_HEIGHT))
//            .extBgColor(color: .clear)
//            .extHidden(hidden: true)
//        
//        let lab = UILabel().easy("暂无离线附件", .gray, .systemFont(ofSize: 16))
//            .extTextAligment(aligment: .center).extNumberOfLines(lines: 0)
//        view.addSubview(lab)
//        lab.snp.makeConstraints { make in
//            make.edges.equalTo(UIEdgeInsets.zero)
//        }
//        
//        return view
//    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        title = "离线附件列表"
        
        tableView.mj_footer = nil
   
        getALLFialClip()
    }
    
    // 获取所有标记为失败的切片
    private func getALLFialClip() {
        
        dataArray = ClipOfflineTool.shared.getClip(status: "-1")
        
        tableView.beginUpdates()
        
        tableView.reloadData()
        
        tableView.endUpdates()
        
        tableView.mj_header?.endRefreshing()
        
        if dataArray.count == 0 {
            KSToast("暂无离线附件", duration: 2)
        }
    }

    
    //MARK: - TableView DataSource & Delegate
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let one = self.dataArray[indexPath.row]
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: GlobalTableViewCell.identifier, for: indexPath) as? GlobalTableViewCell else {
            fatalError()
        }
        
        cell.defaultRightColor = .orange
        cell.operateBtn.setTitle(["重传"], colors: [.orange], btnCTypes: [.border])
        cell.operateBtn.clickAction = { [weak self] (_, _) in
            ClipOfflineTool.shared.uploadOne(model: one) { success, errorInfo in
                if success {
                    ClipOfflineTool.shared.updateUpSatatus(id: one.id, status: "1")
                    
                    self?.getALLFialClip()
                    
                } else {
                    KSToast(errorInfo)
                }
            }
        }
        
        let titleAndStatus: (String,String) = ("", "失败")
  
        var contArr: [(String, String?)]
      
        contArr = [
            ("单据名称", one.menuName),
            ("创建时间", one.offline_dt),
            ("附件名称", one.image.name.unwrap),
            ("切片数量", "\(one.image.clipCount.unwrap)"),
            ("当前切片", one.image.clipName.unwrap)
        ]
        
        if one.failReason.count > 2 {
            contArr.append(("失败原因", one.failReason))
        }

//        cell.setDataSource(contentArr: contArr, titleAndStatus: titleAndStatus)
        cell.setDataSource(contentArr: contArr, btnTitleArr: ["重传"], titleAndStatus: titleAndStatus)
        
        return cell
    }
}
